package org.example.struct.array;

/**
 * @author liyishan
 * @date 2024/1/15 09:48
 * @apiNote
 */

public class Test00028StrStr {
    public static void main(String[] args) {
        String haystack = "mississippi";
        String needle = "issip";
        System.out.println(strStr1(haystack, needle));
    }

    public static int strStr(String haystack, String needle) {
        return haystack.indexOf(needle);
    }


    public static int strStr1(String haystack, String needle) {
        int len1 = haystack.length();
        int len2 = needle.length();
        for(int i = 0; i <= len1 - len2; i++){
            boolean flag = true;
            if(haystack.charAt(i) == needle.charAt(0)){
                for(int j = i;j< i +len2 ;j++){
                    if(haystack.charAt(j) != needle.charAt(j - i)){
                        flag = false;
                        break;
                    }
                }
                if(flag){
                    return i;
                }
            }
        }
        return -1;
    }

}
